<?php
/**
 * This file contains the class InsertTextPlugin
 *
 *@package InsertTextPlugin
 *@license LGLP
 */
class InsertTextPlugin
{
    
    /**
     * The path to the dir with the files to insert
     */
    public $textPath;
    
    public function __construct($params = "") 
    {
        $event = Event::singleton();
        $event->add("build_gui", array($this, "buildMenu"), array());
        $this->textPath = dirname(__FILE__) . "/files";
    }
    
    public function buildMenu($params = "")
    {
        $app = Application::singleton();
        
        $insert = new GtkMenuItem("Insert Text");
        $app->TextEditor->ToolsMenu->append($insert);
        
        $menu = new GtkMenu();
        $insert->set_submenu($menu);
        
        if ($dh = opendir($this->textPath)) {
            $file = readdir($dh);
            while ($file != "") {
                if ($file != "." && $file != "..") {
                    if (is_file($this->textPath . "/" . $file)) {
                        $item = new GtkMenuItem(MyString::removeLast($file, 4));
                        $item->connect("activate", array($this, "onMenuOpen"), $file);
                        $menu->append($item);
                        $menu->show_all();
                    }
                }
                $file = readdir($dh);
            }
        }
        $menu->show_all();
        $app->TextEditor->ToolsMenu->show_all();
    }
    
    public function onMenuOpen($menu, $file)
    {
        $app = Application::singleton();
        $editor = $app->TextEditor->GetEditor();
        $contents = file_get_contents($this->textPath . "/" . $file);
        $editor->InsertText($contents);
    }
}

 
